package com.jf.docsify.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/8/5.1       chenwr    2022/8/5           Create
 * </pre>
 * @date 2022/8/5
 */
public class WebUtil {
    private WebUtil() {
    }


    public static String getIpAddr(HttpServletRequest request) {
        String xForwardedFor = request.getHeader("x-forwarded-for");
        if (StringUtils.isNotEmpty(xForwardedFor) && xForwardedFor.contains(", ")) {
            String[] var2 = xForwardedFor.split(", ");
            xForwardedFor = var2[0];
        }

        if (xForwardedFor == null || xForwardedFor.length() == 0 || "unknown".equalsIgnoreCase(xForwardedFor)) {
            xForwardedFor = request.getHeader("Proxy-Client-IP");
        }

        if (xForwardedFor == null || xForwardedFor.length() == 0 || "unknown".equalsIgnoreCase(xForwardedFor)) {
            xForwardedFor = request.getHeader("WL-Proxy-Client-IP");
        }

        if (xForwardedFor == null || xForwardedFor.length() == 0 || "unknown".equalsIgnoreCase(xForwardedFor)) {
            xForwardedFor = request.getRemoteAddr();
        }

        return dealWithIP(xForwardedFor);
    }

    public static String dealWithIP(String idAddr) {
        return !"127.0.0.1".equals(idAddr) && !"0:0:0:0:0:0:0:1".equals(idAddr) ? idAddr : "localhost";
    }
}